package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@Component
public class SpringSecurityUserService implements UserDetailsService {

    @Reference//使用dubbo远程调用用户服务
    private UserService userService;

    //根据用户名查询用户信息

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {


       //远程调用用户服务，根据用户名查询用户信息
        com.itheima.pojo.User user = userService.findByUsername(username);
        if (user.getStation().equals("0")) {
            return null;
        }
       /* //根据电话号码查询用户信息
        com.itheima.pojo.User user1 = userService.findByTelephone(username);*/

        if (user == null) {
            //用户不存在
            //根据电话号码查询用户信息
            com.itheima.pojo.User user1 = userService.findByTelephone(username);
            if(user1==null){
                return null;
            }

            if (user1.getStation().equals("0")) {
                return null;
            }
            List<GrantedAuthority> list1 = new ArrayList<>();

            Set<Role> roles = user1.getRoles();

            for (Role role : roles) {
                //给当前登陆用户授予角色
                list1.add(new SimpleGrantedAuthority(role.getKeyword()));
                Set<Permission> permissions = role.getPermissions();
                for (Permission permission : permissions) {
                    //给该角色授予相应权限
                    if(permission.getStation().equals("1")) {
                        list1.add(new SimpleGrantedAuthority(permission.getKeyword()));
                   }
                }
            }

            UserDetails userDetails = new User(user1.getUsername(), user1.getPassword(), list1);

            return userDetails;

        }/*else if(user==null){
            user=new com.itheima.pojo.User();
            BeanUtils.copyProperties(user1,user);
        }
        System.out.println(user.getUsername());*/
        //将用户信息返回给框架
        //框架会进行密码比对(页面提交的密码和数据库中查询的密码进行比对)
        List<GrantedAuthority> list = new ArrayList<>();
        Set<Role> roles = user.getRoles();

        for (Role role : roles) {
            //给当前登陆用户授予角色
            list.add(new SimpleGrantedAuthority(role.getKeyword()));
            Set<Permission> permissions = role.getPermissions();
            for (Permission permission : permissions) {
                //给该角色授予相应权限
                if(permission.getStation().equals("1")) {
                    list.add(new SimpleGrantedAuthority(permission.getKeyword()));
                }
            }
        }

        UserDetails userDetails = new User(user.getUsername(), user.getPassword(), list);

        return userDetails;

    }
}
